package com.visionork.android.tai.activity;

import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;

import com.visionork.android.tai.TaiApp;
import com.visionork.android.tai.model.Program;
import com.visionork.android.tai.model.ProgramRepositoryBase;

public class VarietyShowListActivity extends ProgramListActivityBase {

	public VarietyShowListActivity() {
		super();
		// TODO Auto-generated constructor stub
	}

	@Override
	protected ProgramRepositoryBase getRepository() {
		return TaiApp.getShowRepository();
	}

	@Override
	protected void onResume() {
		itemAdapter.notifyDataSetChanged();
		super.onResume();
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.showlist_option_menu, menu);
		return true;
	}

	@Override
	public void onCreateContextMenu(ContextMenu menu, View v,
			ContextMenu.ContextMenuInfo menuInfo) {
		MenuInflater inflater = getMenuInflater();
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) menuInfo;
		Program prog = getRepository().getPrograms().get(info.position);
		if (!prog.isFavorite()) {
			inflater.inflate(R.menu.program_context_menu_add, menu);
		} else {
			inflater.inflate(R.menu.program_context_menu_remove, menu);
		}
	}

	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {
		AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo) item
				.getMenuInfo();		
		switch (item.getItemId()) {
		case R.id.toggle_favorite:
			Program selectedProgram = getRepository().getPrograms().get(info.position);
			selectedProgram.setFavorite(!selectedProgram.isFavorite());
			getRepository().saveToFile();
			itemAdapter.notifyDataSetChanged();
			return true;
		case R.id.share:			
			Program shareedProgram = getRepository().getPrograms().get(info.position);
			String subject = "FWD : " + shareedProgram.getTitle();
			String text = shareedProgram.getTitle() + " - " + shareedProgram.getUrlLink();
			TaiApp.share(this, subject, text);
			return true;
		case R.id.refresh:
			loadData(false);
			getRepository().saveToFile();
			return true;
		}

		return super.onMenuItemSelected(featureId, item);

	}

}
